package com.wjw.rabbitmq.springbootorderrabbitproducer.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 1.模拟用户下单：fanout发布与订阅模式
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrderFanout(String userId,String productId,int num){
        //1.根据商品ID查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);
        //
        /**
         * 3.通过MQ来完成消息的分发
         * 参数1：交换机
         * 参数2：路由Key / 队列名
         * 参数3: 消息内容
         */
        String exchangeName = "fanout_order_exchange";
        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }

    /**
     * 2.模拟用户下单:direct路由模式(路由key)
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrderDirect(String userId,String productId,int num){
        //1.根据商品ID查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);
        //
        /**
         * 3.通过MQ来完成消息的分发
         * 参数1：交换机
         * 参数2：路由Key / 队列名
         * 参数3: 消息内容
         */
        String exchangeName = "direct_order_exchange";
        rabbitTemplate.convertAndSend(exchangeName,"sms",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderId);
    }

    /**
     * 3.模拟用户下单:topic主题模式(模糊路由key)
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrderTopic(String userId,String productId,int num){
        //1.根据商品ID查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);
        //
        /**
         * 3.通过MQ来完成消息的分发
         * 参数1：交换机
         * 参数2：路由Key / 队列名
         * 参数3: 消息内容
         */
        String exchangeName = "topic_order_exchange";
        //rabbitTemplate.convertAndSend(exchangeName,"#.sms.#",orderId);
        //rabbitTemplate.convertAndSend(exchangeName,"*.email.#",orderId);
        //rabbitTemplate.convertAndSend(exchangeName,"com.#",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"com.xxx.sms.xxx",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"com.email",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"com.duanxin",orderId);
    }

    /**
     * 4.模拟用户下单:TTL给队列设置过期时间(消息在队列中存在多久,单位为毫秒)
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrderTTLQueue(String userId,String productId,int num){
        //1.根据商品ID查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);
        //
        /**
         * 3.通过MQ来完成消息的分发
         * 参数1：交换机
         * 参数2：路由Key / 队列名
         * 参数3: 消息内容
         */
        String exchangeName = "ttl_direct_exchange";
        rabbitTemplate.convertAndSend(exchangeName,"ttl",orderId);
    }

    /**
     * 5.模拟用户下单:TTL给消息设置过期时间
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrderTTLMessage(String userId,String productId,int num){
        //1.根据商品ID查询库存是否充足
        //2.保存订单
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功：" + orderId);
        //
        /**
         * 3.通过MQ来完成消息的分发
         * 参数1：交换机
         * 参数2：路由Key / 队列名
         * 参数3: 消息内容
         */
        String exchangeName = "ttl_direct_exchange";
        //给消息设置过期时间
        MessagePostProcessor mpp = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");//设置消息5秒过期
                message.getMessageProperties().setContentEncoding("UTF-8");//设置消息的字符集
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,"ttlmessage",orderId,mpp);
    }
}
